package com.vayne.demo;

import com.vayne.bean.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 注解方式加载 Bean
 * @author xuerui.zhang
 * @since 2020/1/19 上午10:43
 */
public class AnnotationInjectionResolveDemo {

    @Autowired
    private Course course;

    public static void main(String[] args) {
        // 创建应用上下文：BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(AnnotationInjectionResolveDemo.class);

        // 加载资源
        XmlBeanDefinitionReader beanDefinition = new XmlBeanDefinitionReader(applicationContext);
        String path = "classpath:/dependency_lookup_config.xml";
        int count = beanDefinition.loadBeanDefinitions(path);
        System.out.println("加载 Bean 的数量为：" + count);

        // 启动应用上下文
        applicationContext.refresh();

        AnnotationInjectionResolveDemo demo = applicationContext.getBean(AnnotationInjectionResolveDemo.class);
        System.out.println("course: " + demo.course);
    }
}
